// Agent sample in project MAPC

/* Initial beliefs and rules */
{ include("rules.asl") }    

/* Initial goals */

!move. // move to target.

/* Plans */
+!restart 
  <- .print("*** restart ***"); 
     .drop_all_desires;
     .abolish(target_pos(_,_));
     -+last_dir(null);
     !get_new_target; !!move.
     
// Revise plan each 1 steps
+pos(_,_,Step) 
  	: Step mod 1 == 0
  	<- !get_new_target.
     
// If no position is perceived yet, wait until it is and reintroduce goal.		
+!get_new_target
	: not pos(_,_,_)
	<- .wait("+pos(_,_,_)");
		!get_new_target.
	
// let the leader delegate a target to the agent	
+!get_new_target
	:	pos(X,Y,Step)
	&	goal(herd)
	<- 	.send(agent1, achieve, delegate_target(X,Y,Step)).
	
// Don't have a target -> get a new target
+pos(_,_,_) : not target_pos(_,_) <- !get_new_target.	

// no goal
+!move	: 	not goal(_)
		<-	-+goal(herd); !move.		

// doesn't have a target
+!move 	: 	not target_pos(_,_)
		<- 	.wait("+target_pos(_,_)");
			!move.		

// doesn't have a position
+!move 	: 	not pos(_,_,_)
		<- 	.wait("+pos(_,_,_)");
			!move.		

// is at desired target	   	 
+!move 	: 	pos(X,Y,_) 
		& 	target_pos(X,Y) 
		<- 	-target_pos(_,_);  
			do(skip);
			!get_new_target; !!move.			
				
// move towards target
+!move : pos(X,Y,_) & target_pos(DX,DY)
	   <- jia.astar(X,Y,DX,DY,A); do(A); -+last_dir(A);
	   		!!move. // continue moving
	   		